﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;
using System.ComponentModel;
using System.Data.Linq;

namespace DomainModel.Entities
{
    [Table (Name = "Applications")]
    public class Application : IDataErrorInfo
    {
        [Column(Name = "applicationID", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        public int ApplicationID { get; set; }
        [Column(Name = "userID")]
        public Guid UserID { get; set; }
        [Column(Name = "first_name")]
        public string First_Name { get; set; }
        [Column(Name = "last_name")]
        public string Last_Name { get; set; }
        [Column(Name = "middle_name")]
        public string Middle_Name { get; set; }
        [Column(Name = "old_name")]
        public string Old_Name { get; set; }
        [Column(Name = "social_security")]
        public string Social_Security { get; set; }
        [Column(Name = "street")]
        public string Street { get; set; }
        [Column(Name = "city")]
        public string City { get; set; }
        [Column(Name = "us_state")]
        public string State { get; set; }
        [Column(Name = "zipcode")]
        public int Zipcode { get; set; }
        [Column(Name = "phone_number")]
        public string Phone_Number { get; set; }
        [Column(Name = "position1")]
        public int Position1 { get; set; }
        [Column(Name = "position2")]
        public int? Position2 { get; set; }
        [Column(Name = "position3")]
        public int? Position3 { get; set; }
        [Column(Name = "expected_salary")]
        public int Expected_Salary { get; set; }
        [Column(Name = "full_time")]
        public bool Full_Time { get; set; }
        [Column(Name = "days")]
        public bool Days { get; set; }
        [Column(Name = "evenings")]
        public bool Evenings { get; set; }
        [Column(Name = "weekends")]
        public bool Weekends { get; set; }
        [Column(Name = "notes")]
        public string Notes { get; set; }

        [Association(OtherKey = "ApplicationID")]
        private EntitySet<AnsweredQuestion> _answeredQuestions = new EntitySet<AnsweredQuestion>();
        public IList<AnsweredQuestion> AnsweredQuestions { get { return _answeredQuestions.ToList().AsReadOnly(); } }

        [Association(OtherKey = "ApplicationID")]
        private EntitySet<ApplicantProgress> _applicantsProgress = new EntitySet<ApplicantProgress>();
        public IList<ApplicantProgress> ApplicantsProgress { get { return _applicantsProgress.ToList().AsReadOnly(); } }

        public string this[string fieldName]
        {
            get
            {
                if ((fieldName == "First_Name") && string.IsNullOrEmpty(First_Name))
                    return "First Name must not be empty.";
                if ((fieldName == "Last_Name") && string.IsNullOrEmpty(Last_Name))
                    return "Last Name must not be empty.";
                if ((fieldName == "Social_Security") && (string.IsNullOrEmpty(Social_Security) || Social_Security.Length != 9))
                    return "Social security must not be empty, and must be 9 digits.";
                if ((fieldName == "Street") && string.IsNullOrEmpty(Street))
                    return "Street must not be empty.";
                if ((fieldName == "City") && string.IsNullOrEmpty(City))
                    return "City must not be empty.";
                if ((fieldName == "State") && (string.IsNullOrEmpty(State) || State.Length != 2))
                    return "State must not be empty, use 2 letter state abbreviation.";
                if ((fieldName == "Phone_Number") && (string.IsNullOrEmpty(Phone_Number) || Phone_Number.Length != 10))
                    return "Phone number incorrect. Format: 1112223333";
                if ((fieldName == "Zipcode") && (Zipcode <= 10000 || Zipcode >= 100000))
                    return "Zipcode incorrect.  Please enter 5 digit zip code.";
                if ((fieldName == "Expected_Salary") && (Expected_Salary <= 0 || Expected_Salary > 2147483640))
                    return "No Salary Entered.";
                if ((fieldName == "Position1") && Position1 == 0)
                    return "You must pick a job to apply for.";

                return null;
            }
        }

        public string Error { get { return null; } }//Not actually needed, but necessary to implement IDataErrorInfo
    }
}
